package comgame.components;

import com.almasb.fxgl.entity.component.Component;
import comgame.entities.Player;

public class SkillComponent extends Component {
    private int farming = 1;
    private int combatLevel = 1;
    private int combatExp = 0;

    public int getFarming() {
        return farming;
    }

    public void setFarming(int farming) {
        this.farming = farming;
    }

    public int getCombatLevel() {
        return combatLevel;
    }

    public void setCombatLevel(int combatLevel) {
        this.combatLevel = combatLevel;
    }

    public int getCombatExp() {
        return combatExp;
    }

    public void setCombatExp(int combatExp) {
        this.combatExp = combatExp;
    }

    /**
     * 计算升级所需经验值
     * 新公式：20* 等级^2    * 序列：2088020507209801280620     */
    public int getRequiredExpForLevel(int level) {
        if (level <= 1) return 20;
        return 20 * level * level;
    }

    /**
     * 添加战斗经验值并检查是否升级
     * @param exp 获得的经验值
     * @return 是否升级了
     */
    public boolean addCombatExp(Player player, int exp) {
        System.out.println("SkillComponent.addCombatExp - 添加经验: " + exp + ", 当前等级: " + combatLevel + ", 当前经验: " + combatExp);
        int oldExp = combatExp;
        combatExp += exp;
        boolean leveledUp = false;
        while (combatExp >= getRequiredExpForLevel(combatLevel)){
            int requiredExp = getRequiredExpForLevel(combatLevel);
            combatLevel++;
            combatExp -= requiredExp;
            int newMaxHealth = (int) (player.getMaxHealth() * 1.1);
            player.setMaxHealth(newMaxHealth);
            comgame.components.HealthComponent hc = player.getHealthComponent();
            if (hc != null) {
                hc.setMaxHealth(newMaxHealth);
                hc.restoreFullHealth();
            }
            leveledUp = true;
            System.out.println("升级！新等级: " + combatLevel + ", 剩余经验: " + combatExp);
        }
        // 如果升级了，同步属性到组件
        if (leveledUp) {
            player.syncAttributesToComponent();
        }
        System.out.println("SkillComponent.addCombatExp - 最终等级: " + combatLevel + ", 最终经验: " + combatExp);
        return leveledUp;
    }
}
